I have asked to write a program using for loops to create a hollow box but my code is
Is this acceptable? I left a few days before I have to pass up my assignment. Anyone please help?Code:edited to prevent any abuse
I have asked to write a program using for loops to create a hollow box but my code is
Is this acceptable? I left a few days before I have to pass up my assignment. Anyone please help?Code:edited to prevent any abuse
Last edited by exjames1991; 10-20-2009 at 12:10 AM.
I miss out the word "for" in the question, it this acceptable? Anyone can guide me to change it to for loops??
over hereCode:int height; /* initialize height */ int width; /* define width */ int n; /* define n */ printf("Enter the height and width:\n"); /* prints "Enter the height and width:" */ scanf("%d", &n); /* scans an integer and prints the value as n */ height = n; /* assign height */ width = n; /* assign width */
try the change it will work properlyCode:int height; /* initialize height */ int width; /* define width */ int n; /* define n */ int x; printf("Enter the height and width:\n"); /* prints "Enter the height and width:" */ scanf("%d %d", &n,&x) // WHERE BOTH VALUES ARE SPLIT BY A SPACE height = n; /* assign height */ width = x; /* assign width */
I thought for loops have for statements in the code?? Can you assist me to change from while loops to for loops
To use for loops to do this, you need two of them:
That might help.Code:#include <stdio.h> #include <stdlib.h> /* function main begins program execution */ int main () { int height; /* initialize height */ int width; /* define width */ int high; /* define high */ int wide; /* define wide */ int n; /* define n */ printf("\n\n Enter the height and width [3-40]: "); /* prints "Enter the height and width:" */ scanf("%d", &n); /* scans the height and width of the box */ height = width = n; printf("\n\n"); if ((width < 3) || (width > 40)) { printf("An error has occur.\n"); /* prints "An error has occur." */ exit (1); /* exit program if has error */ } for(high = 0; high < height; high++) { printf("\n"); for(wide = 0; wide < width; wide++) { if(high == 0 || high == height-1) printf("*"); else { if(wide == 0 || wide == width - 1) printf("*"); else printf(" "); } } } return 0; /* indicate program ended successfully */ } /* end function main */
Last edited by Adak; 10-19-2009 at 02:16 AM.
Code:edited to prevent any abuse
Last edited by exjames1991; 10-20-2009 at 12:11 AM.
your still missing the main point..here check the scanf statement and printfstatement
chk der...for example if you type..." 30 21" ..look the scanf statement will read only 30Code:printf("Enter the height and the width\n"); scanf("%d", &n); height = n; width = n;
and will put height and width as 30...
in order to correct it... try
Code:int x; printf("Enter the height and the width\n"); scanf("%d %d", &x,&n); height = n; width = x;
Last edited by Obelisk; 10-19-2009 at 01:53 AM.
ExJames:
======
See post #6 for the update
@ Adak THX(edited)
Means that I have to enter 1 time which is the value of n to determine both of its height and width. Eg, I enter 5, it prints both 5 height and width. But what Adak wrote, I have to enter 2 different numbers to get different height and width. Eg, I enter 5 6, it prints 5 height and 6 width. Unless I misinterpret the question wrongly. Else if Adak mind to change the code abit so that when I enter 1 value it equals to both height and width?
Last edited by exjames1991; 10-19-2009 at 04:42 AM.